home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyFileSystem.p < prev    next >
Encoding:
Text File  |  1994-08-04  |  7.5 KB  |  328 lines  |  [TEXT/PJMM]

  1. unit MyFileSystem;
  2.  
  3. interface
  4.  
  5.     const
  6.         PAvailable = fsCurPerm;
  7.         PIn = fsRdPerm;
  8.         POut = fsWrPerm;
  9.         PInOut = fsRdWrPerm;
  10.         PShared = fsRdWrShPerm;
  11.         buf_size = 2048;
  12.         eof_byte = $1A;
  13.  
  14.     type
  15.         bufferArray = packed array[0..buf_size] of byte;
  16.         bufferPtr = ^bufferArray;
  17.         bufferHandle = ^bufferPtr;
  18.         MFSfile = record
  19.                 reading: boolean;
  20.                 rn: integer;
  21.                 buf_len, buf_pos: longInt;
  22.                 eof: boolean;
  23.                 length: longInt;
  24.                 buf: bufferHandle;
  25.             end;
  26.  
  27.     function MFSExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  28.     function MFSDirExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  29.     procedure MFSUniqueName (wdrn: integer; dirID: longInt; var name: str63);
  30.     function MFSOpenIn (wdrn: integer; dirID: longInt; name: str255; var thefile: MFSfile): OSErr;
  31.     function MFSCreate (wdrn: integer; dirID: longInt; name: str255; c, t: OSType): OSErr;
  32. {    function MFSDelete (wdrn: integer; dirID: longInt; name: str255): OSErr;}
  33. { use HDelete instead}
  34.     function MFSOpenOutDF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  35.     function MFSOpenOutRF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  36.     function MFSEof (var thefile: MFSfile): boolean;
  37.     function MFSLength (var thefile: MFSfile): longInt;
  38.     function MFSReadByte (var thefile: MFSfile; var b: byte): OSErr;
  39.     function MFSWriteByte (var thefile: MFSfile; b: byte): OSErr;
  40.     function MFSClose (var thefile: MFSfile): OSErr;
  41.     function MFSOpenDF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  42.     function MFSOpenRF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  43. { perm = fsCurPerm, fsRdPerm, fsWrPerm, fsRdWrPerm, fsRdWrShPerm }
  44.     procedure SegmentMFSByte;
  45.     procedure SegmentMFS;
  46.  
  47. implementation
  48.  
  49.     uses
  50.         MyTypes;
  51.  
  52. {$S MFSByte}
  53.     procedure SegmentMFSByte;
  54.     begin
  55.     end;
  56.  
  57. {$S MFS}
  58.     procedure SegmentMFS;
  59.     begin
  60.     end;
  61.  
  62. {$S MFSByte}
  63.     procedure InitTheFile (var thefile: MFSfile);
  64.     begin
  65.         thefile.buf := bufferHandle(NewHandle(buf_size));
  66.     end;
  67.  
  68. {$S MFS}
  69.     function MFSExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  70.         var
  71.             pb: HParamBlockRec;
  72.     begin
  73.         with pb do begin
  74.             ioNamePtr := @name;
  75.             ioVRefNum := wdrn;
  76.             ioDirID := dirID;
  77.             ioFDirIndex := 0;
  78.         end;
  79.         MFSExists := PBHGetFInfo(@pb, false) = noErr;
  80.     end;
  81.  
  82. {$S MFS}
  83.     function MFSDirExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  84.         var
  85.             pb: HParamBlockRec;
  86.             oe: OSErr;
  87.     begin
  88.         with pb do begin
  89.             ioNamePtr := @name;
  90.             ioVRefNum := wdrn;
  91.             ioDirID := dirID;
  92.             if name = '' then
  93.                 ioFDirIndex := -1
  94.             else
  95.                 ioFDirIndex := 0;
  96.         end;
  97.         oe := PBGetCatInfo(@pb, false);
  98.         MFSDirExists := (oe = noErr) and (BAND(pb.ioFlAttrib, $0010) <> 0);
  99.     end;
  100.  
  101. {$S MFS}
  102.     procedure MFSUniqueName (wdrn: integer; dirID: longInt; var name: str63);
  103.         var
  104.             base: str31;
  105.             n: integer;
  106.     begin
  107.         if MFSExists(wdrn, dirID, name) then begin
  108.             base := Concat(Copy(name, 1, 27), '#');
  109.             n := 1;
  110.             repeat
  111.                 name := Concat(base, chr(n div 100 + 48), chr(n div 10 mod 10 + 48), chr(n mod 10 + 48));
  112.                 n := n + 1;
  113.             until not MFSExists(wdrn, dirID, name);
  114.         end;
  115.     end;
  116.  
  117. {$S MFSByte}
  118.     function MFSOpenIn (wdrn: integer; dirID: longInt; name: str255; var thefile: MFSfile): OSErr;
  119.     begin
  120.         InitTheFile(thefile);
  121.         with thefile do begin
  122.             reading := true;
  123.             buf_pos := 0;
  124.             buf_len := 0;
  125.             MFSOpenIn := MFSOpenDF(rn, wdrn, dirID, name, PIn);
  126.             if GetEOF(rn, length) <> noErr then
  127.                 length := 0;
  128.             eof := length = 0;
  129.         end;
  130.     end;
  131.  
  132. {$S MFS}
  133.     function MFSCreate (wdrn: integer; dirID: longInt; name: str255; c, t: OSType): OSErr;
  134.         var
  135.             ooe, oe: integer;
  136.             fi: Finfo;
  137.     begin
  138.         oe := HCreate(wdrn, dirID, name, c, t);
  139.         if oe = dupFNErr then begin
  140.             ooe := HGetFInfo(wdrn, dirID, name, fi);
  141.             oe := HDelete(wdrn, dirID, name);
  142.             oe := HCreate(wdrn, dirID, name, c, t);
  143.             if (oe = noErr) and (ooe = noErr) then begin
  144.                 fi.fdType := t;
  145.                 fi.fdCreator := c;
  146.                 ooe := HSetFInfo(wdrn, dirID, name, fi);
  147.             end;
  148.         end;
  149.         MFSCreate := oe;
  150.     end;
  151.  
  152. {$S MFSByte}
  153.     function MFSOpenOutDF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  154.         var
  155.             oe: integer;
  156.     begin
  157.         InitTheFile(thefile);
  158.         with thefile do begin
  159.             reading := false;
  160.             oe := MFSCreate(wdrn, dirID, name, c, t);
  161.             if oe = noErr then
  162.                 oe := MFSOpenDF(rn, wdrn, dirID, name, POut);
  163.             buf_pos := 0;
  164.             buf_len := 0;
  165.             length := 0;
  166.             eof := false;
  167.             MFSOpenOutDF := oe;
  168.         end;
  169.     end;
  170.  
  171. {$S MFSByte}
  172.     function MFSOpenOutRF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  173.         var
  174.             oe: integer;
  175.     begin
  176.         InitTheFile(thefile);
  177.         with thefile do begin
  178.             reading := false;
  179.             oe := MFSCreate(wdrn, dirID, name, c, t);
  180.             if oe = dupFNErr then
  181.                 oe := noErr;
  182.             if oe = noErr then
  183.                 oe := MFSOpenRF(rn, wdrn, dirID, name, POut);
  184.             buf_pos := 0;
  185.             buf_len := 0;
  186.             length := 0;
  187.             eof := false;
  188.             MFSOpenOutRF := oe;
  189.         end;
  190.     end;
  191.  
  192. {$S MFSByte}
  193.     function MFSLength (var thefile: MFSfile): longInt;
  194.     begin
  195.         MFSLength := thefile.length;
  196.     end;
  197.  
  198. {$S MFSByte}
  199.     function MFSEof (var thefile: MFSfile): boolean;
  200.     begin
  201.         MFSEof := thefile.eof;
  202.     end;
  203.  
  204. {$S MFSByte}
  205.     function MFSReadByte (var thefile: MFSfile; var b: byte): OSErr;
  206.         var
  207.             oe: OSErr;
  208.         procedure Read;
  209.         begin
  210.             with thefile do begin
  211.                 buf_pos := 0;
  212.                 buf_len := buf_size;
  213.                 oe := FSRead(rn, buf_len, POINTER(buf^));
  214.                 if oe = eofErr then
  215.                     oe := noErr;
  216.                 if buf_len = 0 then
  217.                     oe := eofErr;
  218.                 if oe <> noErr then begin
  219.                     buf_len := 0;
  220.                     eof := true;
  221.                 end;
  222.             end;
  223.         end;
  224.     begin
  225.         with thefile do
  226.             if reading then begin
  227.                 if eof then begin
  228.                     b := eof_byte;
  229.                     MFSReadByte := eofErr;
  230.                 end
  231.                 else begin
  232.                     oe := noErr;
  233.                     if buf_pos = buf_len then
  234.                         Read;
  235.                     MFSReadByte := oe;
  236.                     if oe = noErr then begin
  237.                         b := buf^^[buf_pos];
  238.                         buf_pos := buf_pos + 1;
  239.                         if buf_pos = buf_len then
  240.                             Read;
  241.                     end;
  242.                 end;
  243.             end
  244.             else
  245.                 MFSReadByte := paramErr;
  246.     end;
  247.  
  248. {$S MFSByte}
  249.     function Flush (var thefile: MFSfile): OSErr;
  250.         var
  251.             count: longInt;
  252.             oe: integer;
  253.     begin
  254.         with thefile do begin
  255.             count := buf_pos;
  256.             if count = 0 then
  257.                 oe := noErr
  258.             else
  259.                 oe := FSWrite(rn, count, POINTER(buf^));
  260.             if count <> buf_pos then
  261.                 oe := ioErr;
  262.             buf_len := 0;
  263.             buf_pos := 0;
  264.         end;
  265.         Flush := oe;
  266.     end;
  267.  
  268. {$S MFSByte}
  269.     function MFSWriteByte (var thefile: MFSfile; b: byte): OSErr;
  270.     begin
  271.         with thefile do
  272.             if not reading then begin
  273.                 buf^^[buf_pos] := b;
  274.                 buf_pos := buf_pos + 1;
  275.                 if buf_pos = buf_size then
  276.                     MFSWriteByte := Flush(thefile)
  277.                 else
  278.                     MFSWriteByte := noErr;
  279.             end
  280.             else
  281.                 MFSWriteByte := paramErr;
  282.     end;
  283.  
  284. {$S MFSByte}
  285.     function MFSClose (var thefile: MFSfile): OSErr;
  286.         var
  287.             oe: integer;
  288.     begin
  289.         if not thefile.reading then
  290.             oe := Flush(thefile);
  291.         MFSClose := FSClose(thefile.rn);
  292.         thefile.rn := 0;                { Never close a file twice }
  293.         DisposHandle(handle(thefile.buf));
  294.     end;
  295.  
  296. {$S MFS}
  297.     function MFSOpenDF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  298.         var
  299.             pb: HParamBlockRec;
  300.     begin
  301.         with pb do begin
  302.             ioNamePtr := @name;
  303.             ioVRefNum := wdrn;
  304.             ioPermssn := perm;
  305.             ioMisc := nil;
  306.             ioDirID := dirID;
  307.             MFSOpenDF := PBHOpen(@pb, false);
  308.             rn := ioRefNum;
  309.         end;
  310.     end;
  311.  
  312. {$S MFS}
  313.     function MFSOpenRF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  314.         var
  315.             pb: HParamBlockRec;
  316.     begin
  317.         with pb do begin
  318.             ioNamePtr := @name;
  319.             ioVRefNum := wdrn;
  320.             ioPermssn := perm;
  321.             ioMisc := nil;
  322.             ioDirID := dirID;
  323.             MFSOpenRF := PBHOpenRF(@pb, false);
  324.             rn := ioRefNum;
  325.         end;
  326.     end;
  327.  
  328. end.